home *** CD-ROM | disk | FTP | other *** search
- Path: sun001.spd.dsccc.com!spd!jmccarty
- From: jmccarty@spd.dsccc.com (Mike McCarty)
- Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
- Subject: Re: Tough FACTORIAL math problem...
- Date: 15 Feb 1996 19:15:26 GMT
- Organization: DSC Communications Corporation, Plano, Texas USA
- Message-ID: <4g00oe$ghe@sun001.spd.dsccc.com>
- References: <4fr8be$ass@news.iconn.net> <4ft2uv$575@crl.crl.com>
- NNTP-Posting-Host: aplo139.spd.dsccc.com
-
- In article <4ft2uv$575@crl.crl.com>, Robert Fry <bobfry@crl.com> wrote:
- )thecrow@iconn.net (The Crow) writes:
- )
- )>Here is what I am trying to do, I want to find the last NON-ZERO digit of a
- )>given factorial. For instance,
- )
- )>5! = 120
- )
- )>so the last non-zero digit is 2. I want to be able to do this up to 1000.
- )>Problem is, no matter how huge of a data-type you use, there isn't any way for
- )>the computer to handle 1000!, it is however possible to find the last non-zero
- )>digit somehow. One thing I have tried is as I went through mulitplying the
- )>series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the
- )>trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i
- )>am not really sure why. If anyone has a clue how I can do this let me know.
- )
- )As you already know, the number of digits in a factorial number grows
- )very quickly. I calculated and printed out 1000! several years ago, and
- )it more than fills a page with digits. However, since all you want to do
- )is keep track of the last non-zero digit, why don't you simply perform
- )the multiplications and keep just the last digit? I.e. 3!=6,
- )4!=4*3!=4*6=24 and then just store the 4.
- )
- )This way you could use plain ints or longs, and your interim result
- )(before dropping down to the last nonzero digit) will never be greater
- )than 9x for x!.
-
- Nice try, but no cigar.
-
- 4! = 24 ~ 4
- 5! = 5x4! ~ 5x4 = 20 ~ 0
-
- Now you no longer have the last NON ZERO digit.
-
- Mike
- ----
- char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
-
- I don't speak for DSC. <- They make me say that.
-